package com.invengo.library_rfid.analyze.utils.uhf.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.time.*;
import java.util.Date;
import java.util.Locale;

public class Convert {
//    public static short ToInt16(String value) {
//        if (value == null)
//            return 0;
//        return Short.parseShort(value);
//    }

    public static short ToUInt16(String value) {
        if (value == null)
            return 0;
        return Short.parseShort(value);
    }

//    public static short ToInt16(char value) {
//        if (value > 0x7FFF)
//            throw new IllegalArgumentException(value + "xxxx");
//        return (short) value;
//    }

    public static String ToInt16String(char value) {
        if (value > 0x7FFF)
            throw new IllegalArgumentException(value + "xxxx");
        return String.valueOf(value);
    }

//    public static char ToChar(int value) {
//        if (value > 0x7FFF)
//            throw new IllegalArgumentException();
//        return (char) value;
//    }
//    public static char ToChar(String value) {
//        if (value == null)
//            throw new IllegalArgumentException();
//
//        if (value.length() != 1)
//            throw new IllegalArgumentException();
//
//        return value.charAt(0);
//    }

//    public static long ToInt64(String value) {
//        if (value == null)
//            return 0;
//        return Long.parseLong(value);
//    }



//    public static int ToInt32(char value) {
//        return (int) value;
//    }
//
//    public static int ToInt32(String value) {
//        if (value == null)
//            return 0;
//        return Integer.parseInt(value);
//    }

//    public static byte ToByte(char value) {
//        if (value > 0x7FFF) throw new IllegalArgumentException();
//        return (byte) value;
//    }
//
//    public static byte ToByte(String value) {
//        if (value == null)
//            return 0;
//        return Byte.parseByte(value);
//    }

    public static String ToString(int value) {
        return Character.toString((char) value);
    }
    public static String ToString(long  value,int toBase){
        switch (toBase){
            case 2:
                return Long.toBinaryString(value);
            default:
                return "";
        }
    }


    public static byte[] GetBytesAscii(String value) {
        try {
            return value.getBytes("US-ASCII");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static String GetAsciiString(byte[] bytes) {
        StringBuffer tStringBuf = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            char c = (char) bytes[i];
            tStringBuf.append(c);
        }
        return tStringBuf.toString();
    }
//    =============================================

    public static short ToInt16(Object value) {
        return value == null ? (short) 0 : ((Number) value).shortValue();
    }

    public static short ToInt16(Object value, Locale provider) {
        return value == null ? (short) 0 : ((Number) value).shortValue();
    }

    public static short ToInt16(boolean value) {
        return value ? (short) 1 : (short) 0;
    }

    public static short ToInt16(char value) {
        if (value > Short.MAX_VALUE) {
            throw new IllegalArgumentException("越界");
        }
        return (short) value;
    }

    public static short ToInt16(byte value) {
        return value;
    }

    public static short ToInt16(short value) {
        return value;
    }

    public static short ToInt16(int value) {
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            throw new IllegalArgumentException("越界");
        }
        return (short) value;
    }

    public static short ToInt16(long value) {
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            throw new IllegalArgumentException("越界");
        }
        return (short) value;
    }

    public static short ToInt16(float value) {
        return ToInt16((double) value);
    }

    public static short ToInt16(double value) {
        return ToInt16((int) value);
    }

    public static short ToInt16(BigDecimal value) {
        return value.shortValueExact();
    }

    public static short ToInt16(String value) {
        return Short.parseShort(value);
    }

    public static short ToInt16(String value, Locale provider) {
        return Short.parseShort(value);
    }

    public static short ToInt16(Date value) {
        return ToInt16(value.toInstant());
    }

    public static short ToInt16(Instant value) {
        try {
            return ToInt16(LocalDateTime.ofInstant(value, ZoneId.systemDefault()));
        } catch (DateTimeException e) {
            throw new IllegalArgumentException("不可能");
        }
    }

    public static short ToInt16(LocalDate value) {
        return ToInt16(value.atStartOfDay());
    }

    public static short ToInt16(LocalDateTime value) {
        return ToInt16(value.toInstant(ZoneOffset.UTC));
    }

    public static short ToInt16(LocalTime value) {
        throw new IllegalArgumentException("不可能");
    }

    public static short ToInt16(OffsetDateTime value) {
        return ToInt16(value.toInstant());
    }

    public static short ToInt16(OffsetTime value) {
        throw new IllegalArgumentException("不可能");
    }

    public static short ToInt16(ZonedDateTime value) {
        return ToInt16(value.toInstant());
    }

    public static char ToChar(Object value) {
        return value == null ? '\0' : ((Character) value).charValue();
    }

    public static char ToChar(Object value, Locale provider) {
        return value == null ? '\0' : ((Character) value).charValue();
    }

    public static char ToChar(boolean value) {
        return value ? '1' : '0';
    }

    public static char ToChar(char value) {
        return value;
    }

    public static char ToChar(byte value) {
        return (char) value;
    }

    public static char ToChar(short value) {
        return (char) value;
    }

    public static char ToChar(int value) {
        return (char) value;
    }

    public static char ToChar(long value) {
        return (char) value;
    }

    public static char ToChar(float value) {
        return ToChar((double) value);
    }

    public static char ToChar(double value) {
        return ToChar((int) value);
    }

    public static char ToChar(BigDecimal value) {
        return (char) value.intValueExact();
    }

    public static char ToChar(String value) {
        if (value == null) {
            throw new NullPointerException();
        }
        if (value.length() != 1) {
            throw new IllegalArgumentException("越界");
        }
        return value.charAt(0);
    }

    public static char ToChar(String value, Locale provider) {
        return ToChar(value);
    }

    public static char ToChar(Date value) {
        return ToChar(value.toInstant());
    }

    public static char ToChar(Instant value) {
        try {
            return ToChar(LocalDateTime.ofInstant(value, ZoneId.systemDefault()));
        } catch (DateTimeException e) {
            throw new IllegalArgumentException("不可能");
        }
    }

    public static char ToChar(LocalDate value) {
        return ToChar(value.atStartOfDay());
    }

    public static char ToChar(LocalDateTime value) {
        return ToChar(value.toInstant(ZoneOffset.UTC));
    }

    public static char ToChar(LocalTime value) {
        throw new IllegalArgumentException("不可能");
    }

    public static char ToChar(OffsetDateTime value) {
        return ToChar(value.toInstant());
    }

    public static char ToChar(OffsetTime value) {
        throw new IllegalArgumentException("不可能");
    }

    public static char ToChar(ZonedDateTime value) {
        return ToChar(value.toInstant());
    }

    public static int ToInt32(Object value) {
        return value == null ? 0 : ((Number) value).intValue();
    }

    public static int ToInt32(Object value, Locale provider) {
        return value == null ? 0 : ((Number) value).intValue();
    }

    public static int ToInt32(boolean value) {
        return value ? 1 : 0;
    }

    public static int ToInt32(char value) {
        return value;
    }

    public static int ToInt32(byte value) {
        return value;
    }

    public static int ToInt32(short value) {
        return value;
    }

    public static int ToInt32(int value) {
        return value;
    }

    public static int ToInt32(long value) {
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("越界");
        }
        return (int) value;
    }

    public static int ToInt32(float value) {
        return ToInt32((double) value);
    }

    public static int ToInt32(double value) {
        if (value >= 0) {
            if (value < 2147483647.5) {
                int result = (int) value;
                double dif = value - result;
                if (dif > 0.5 || dif == 0.5 && (result & 1) != 0) {
                    result++;
                }
                return result;
            }
        } else {
            if (value >= -2147483648.5) {
                int result = (int) value;
                double dif = value - result;
                if (dif < -0.5 || dif == -0.5 && (result & 1) != 0) {
                    result--;
                }
                return result;
            }
        }
        throw new IllegalArgumentException("越界");
    }

    public static int ToInt32(BigDecimal value) {
        return value.intValueExact();
    }

    public static int ToInt32(String value) {
        return Integer.parseInt(value);
    }

    public static int ToInt32(String value, Locale provider) {
        return Integer.parseInt(value);
    }

    public static int ToInt32(Date value) {
        return ToInt32(value.toInstant());
    }

    public static int ToInt32(Instant value) {
        try {
            return ToInt32(LocalDateTime.ofInstant(value, ZoneId.systemDefault()));
        } catch (DateTimeException e) {
            throw new IllegalArgumentException("不可能");
        }
    }

    public static int ToInt32(LocalDate value) {
        return ToInt32(value.atStartOfDay());
    }

    public static int ToInt32(LocalDateTime value) {
        return ToInt32(value.toInstant(ZoneOffset.UTC));
    }

    public static int ToInt32(LocalTime value) {
        throw new IllegalArgumentException("不可能");
    }

    public static int ToInt32(OffsetDateTime value) {
        return ToInt32(value.toInstant());
    }

    public static int ToInt32(OffsetTime value) {
        throw new IllegalArgumentException("不可能");
    }

    public static int ToInt32(ZonedDateTime value) {
        return ToInt32(value.toInstant());
    }

    public static byte ToByte(Object value) {
        return value == null ? 0 : ((Number) value).byteValue();
    }

    public static byte ToByte(Object value, Locale provider) {
        return value == null ? 0 : ((Number) value).byteValue();
    }

    public static byte ToByte(boolean value) {
        return value ? (byte) 1 : (byte) 0;
    }

    public static byte ToByte(byte value) {
        return value;
    }

    public static byte ToByte(char value) {
        if (value > Byte.MAX_VALUE) {
            throw new IllegalArgumentException("越界");
        }
        return (byte) value;
    }

    public static byte ToByte(short value) {
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            throw new IllegalArgumentException("越界");
        }
        return (byte) value;
    }

    public static byte ToByte(int value) {
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            throw new IllegalArgumentException("越界");
        }
        return (byte) value;
    }

    public static byte ToByte(long value) {
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            throw new IllegalArgumentException("越界");
        }
        return (byte) value;
    }

    public static byte ToByte(float value) {
        return ToByte((double) value);
    }

    public static byte ToByte(double value) {
        return ToByte((int) value);
    }

    public static byte ToByte(BigDecimal value) {
        return value.byteValueExact();
    }

    public static byte ToByte(String value) {
        return Byte.parseByte(value);
    }

    public static byte ToByte(String value, Locale provider) {
        return Byte.parseByte(value);
    }

    public static byte ToByte(Date value) {
        return ToByte(value.toInstant());
    }

    public static byte ToByte(Instant value) {
        try {
            return ToByte(LocalDateTime.ofInstant(value, ZoneId.systemDefault()));
        } catch (DateTimeException e) {
            throw new IllegalArgumentException("不可能");
        }
    }

    public static byte ToByte(LocalDate value) {
        return ToByte(value.atStartOfDay());
    }

    public static byte ToByte(LocalDateTime value) {
        return ToByte(value.toInstant(ZoneOffset.UTC));
    }

    public static byte ToByte(LocalTime value) {
        throw new IllegalArgumentException("不可能");
    }

    public static byte ToByte(OffsetDateTime value) {
        return ToByte(value.toInstant());
    }

    public static byte ToByte(OffsetTime value) {
        throw new IllegalArgumentException("不可能");
    }

    public static byte ToByte(ZonedDateTime value) {
        return ToByte(value.toInstant());
    }

    public static long ToInt64(Object value) {
        return value == null ? 0 : ((Number) value).longValue();
    }

    public static long ToInt64(Object value, Locale provider) {
        return value == null ? 0 : ((Number) value).longValue();
    }

    public static long ToInt64(boolean value) {
        return value ? 1 : 0;
    }

    public static long ToInt64(char value) {
        return value;
    }

    public static long ToInt64(byte value) {
        return value;
    }

    public static long ToInt64(short value) {
        return value;
    }

    public static long ToInt64(int value) {
        return value;
    }

    public static long ToInt64(long value) {
        return value;
    }

    public static long ToInt64(float value) {
        return ToInt64((double) value);
    }

    public static long ToInt64(double value) {
        return (long) value;
    }

    public static long ToInt64(BigDecimal value) {
        return value.longValueExact();
    }

    public static long ToInt64(String value) {
        return Long.parseLong(value);
    }

    public static long ToInt64(String value, Locale provider) {
        return Long.parseLong(value);
    }

    public static long ToInt64(Date value) {
        return ToInt64(value.toInstant());
    }

    public static long ToInt64(Instant value) {
        try {
            return ToInt64(LocalDateTime.ofInstant(value, ZoneId.systemDefault()));
        } catch (DateTimeException e) {
            throw new IllegalArgumentException("不可能");
        }
    }

    public static long ToInt64(LocalDate value) {
        return ToInt64(value.atStartOfDay());
    }

    public static long ToInt64(LocalDateTime value) {
        return ToInt64(value.toInstant(ZoneOffset.UTC));
    }

    public static long ToInt64(LocalTime value) {
        throw new IllegalArgumentException("不可能");
    }

    public static long ToInt64(OffsetDateTime value) {
        return ToInt64(value.toInstant());
    }

    public static long ToInt64(OffsetTime value) {
        throw new IllegalArgumentException("不可能");
    }

    public static long ToInt64(ZonedDateTime value) {
        return ToInt64(value.toInstant());
    }

}
